﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace 一键生成
{
    public partial class frmOutput : Form
    {
        public frmOutput()
        {
            InitializeComponent();
        }
        string str = "Data Source=192.168.1.214;Initial Catalog=HSXH;User ID=sa;Password=Ningtu2015";
        string newFileName, FilePath; //路径名
        string jdkz;//基点考证参数
        string jdkzrq;//基点考证日期
        string jdgckz1;//基点高程考证起始日期
        string jdgckz2;//基点高程考证上次日期
        string jdgckz3;//基点高程考证本次日期
        string jdgccg1;//垂直位移观测起始日期
        string jdgccg2;//垂直位移观测上次日期
        string jdgccg3;//垂直位移观测本次日期

        List<string> cs6 = new List<string>();
        List<string> cs7 = new List<string>();
        List<string> cs11 = new List<string>();
        List<string> cs12 = new List<string>();
        List<string> cs13 = new List<string>();
        System.Data.DataTable dt2;//C.S.1上	0+015 的个数
        string dmzd;
        string dmdata;//断面日期参数
        string bhtj;//变化统计


        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //SolidBrush b = new SolidBrush(this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor);
            //e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), this.dataGridView1.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            // string s = string.Empty;
            // for (int i = 0; i < checkedListBox1.Items.Count; i++)
            // {
            //     if (checkedListBox1.GetItemChecked(i))//是否选中
            //     {
            //         s += checkedListBox1.Items[i].ToString();
            //     }
            // }
            //MessageBox.Show(s);
        }
        #region 全选
        private void button1_Click(object sender, EventArgs e)
        {
            checkBox1.Checked = true;
            checkBox2.Checked = true;
            checkBox3.Checked = true;
            checkBox4.Checked = true;
            checkBox5.Checked = true;
            checkBox6.Checked = true;
            checkBox7.Checked = true;
            checkBox8.Checked = true;
            checkBox9.Checked = true;
            checkBox10.Checked = true;
            checkBox11.Checked = true;
            checkBox12.Checked = true;
        }
        #endregion

        #region  清空
        private void button2_Click(object sender, EventArgs e)
        {
            checkBox1.Checked = false;
            checkBox2.Checked = false;
            checkBox3.Checked = false;
            checkBox4.Checked = false;
            checkBox5.Checked = false;
            checkBox6.Checked = false;
            checkBox7.Checked = false;
            checkBox8.Checked = false;
            checkBox9.Checked = false;
            checkBox10.Checked = false;
            checkBox11.Checked = false;
            checkBox12.Checked = false;
        }
        #endregion

        #region  frmOutput_Load
        private void frmOutput_Load(object sender, EventArgs e)
        {
            string sql = "select BrakeName from brake_info";
            System.Data.DataTable dt = GetDataTable(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                comboBox1.Items.Add(dt.Rows[i][0].ToString());
                //MessageBox.Show();
            }
        }
        #endregion

        #region 执行查询语句，返回DataTable
        /// <summary>
        /// 执行查询语句，返回DataTable
        /// </summary>
        /// <param name="sql">查询语句</param>
        /// <returns>DataTable</returns>
        public System.Data.DataTable GetDataTable(string sql)
        {
            using (SqlConnection conn = new SqlConnection(str))
            {
                System.Data.DataTable dt = new System.Data.DataTable();
                try
                {
                    SqlDataAdapter command = new SqlDataAdapter(sql, conn);
                    command.Fill(dt);
                }
                catch (SqlException ex)
                {

                    throw new Exception(ex.Message);
                }
                return dt;
            }
        }
        #endregion

        #region  checkBox2
        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            string sql = "select  distinct JdConfirmDate from jd_confirm where BrakeId ='" + comboBox1.Text + "' ORDER BY JdConfirmDate desc";
            System.Data.DataTable dt = GetDataTable(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                comboBox2.Items.Add(dt.Rows[i][0].ToString());
                comboBox3.Items.Add(dt.Rows[i][0].ToString());
                comboBox4.Items.Add(dt.Rows[i][0].ToString());
            }
        }
        #endregion

        #region  checkBox4
        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            string sql = "select  distinct ObserDate from result_observe where [BreakName] ='" + comboBox1.Text + "' ORDER BY ObserDate desc";
            System.Data.DataTable dt = GetDataTable(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                comboBox6.Items.Add(dt.Rows[i][0].ToString());
                comboBox13.Items.Add(dt.Rows[i][0].ToString());
                comboBox14.Items.Add(dt.Rows[i][0].ToString());

            }
        }
        #endregion

        #region
        private void checkBox9_CheckedChanged(object sender, EventArgs e)
        {
            string sql1 = "select obdate from DM_GCK where BrakeId='" + comboBox1.Text + "'";
            System.Data.DataTable dt1 = GetDataTable(sql1);
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                comboBox11.Items.Add(dt1.Rows[i][0].ToString());
            }
        }
        #endregion

        #region  选择保存路径
        private void button5_Click(object sender, EventArgs e)
        {
            string localFilePath = "";
            SaveFileDialog sfd = new SaveFileDialog();
            //设置文件类型 
            sfd.Filter = "Excel表格（*.xls）|*.xls";

            //设置默认文件类型显示顺序 
            sfd.FilterIndex = 1;

            //保存对话框是否记忆上次打开的目录 
            sfd.RestoreDirectory = true;

            //点了保存按钮进入 
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                localFilePath = sfd.FileName.ToString(); //获得文件路径 
                string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1); //获取文件名，不带路径

                //获取文件路径，不带文件名 
                //FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\")); 
                //给文件名前加上时间 
                //newFileName = DateTime.Now.ToString("yyyy") + fileNameExt; 
                //在文件名里加字符 
                //saveFileDialog1.FileName.Insert(1,"dameng"); 
                //System.IO.FileStream fs = (System.IO.FileStream)sfd.OpenFile();//输出文件
                ////fs输出带文字或图片的文件，就看需求了 
            }
            //C:\Users\Administrator\Desktop\11.xls
            textBox2.Text = localFilePath;
            //return localFilePath;
        }
        #endregion

        #region  垂直位移观测线路示意图
        private void checkBox13_CheckedChanged(object sender, EventArgs e)
        {
            String chartPath = @"E:\淮沐新河项目\工作\输出成果图\" + comboBox1.Text + "\\垂直观测线路示意图";

            //第一种方法
            var files = Directory.GetFiles(chartPath, "*.*");
            foreach (var file in files)
                cs13.Add(file);
        }
        #endregion

        #region    一键生成
        private void button4_Click(object sender, EventArgs e)
        {
            string strtext = "";//工作说明
            //基点考证参数
            if (checkBox1.Checked)
            {
                jdkz = comboBox1.Text;
                jdkzrq = comboBox12.Text;
            }
            else
            {
                jdkz = "0";
                jdkzrq = "0";
            }

            //测量线路示意图参数 cs13

            //基点高程考证参数
            if (checkBox2.Checked)
            {
                jdgckz1 = comboBox2.Text;
                jdgckz2 = comboBox3.Text;
                jdgckz3 = comboBox4.Text;
            }
            else
            {
                jdgckz1 = "0";
                jdgckz2 = "0";
                jdgckz3 = "0";
            }

            //基点观测成果表参数
            if (checkBox4.Checked)
            {
                jdgccg1 = comboBox6.Text;
                jdgccg2 = comboBox13.Text;
                jdgccg3 = comboBox14.Text;
            }
            else
            {
                jdgccg1 = "0";
                jdgccg2 = "0";
                jdgccg3 = "0";
            }


            //断面参数
            if (checkBox9.Checked)
            {
                dmdata = comboBox11.Text;//断面日期参数
                string sql = "select DmId,DmMile from dm_info where BrakeId='" + comboBox1.Text + "'";
                dt2 = GetDataTable(sql);
            }
            else
            {
                dmdata = "0";
                string sql = "select DmId,DmMile from dm_info where BrakeId='" + comboBox1.Text + "'";
                dt2 = GetDataTable(sql);
            }

            //断面桩顶高程
            if (checkBox8.Checked)
            {
                dmzd=comboBox1.Text;
            }
            else
            {
                dmzd = "0";
            }

            //
            if (checkBox5.Checked)
            {
                bhtj = comboBox1.Text;
            }
            else
            {
                bhtj = "0";
            }


            string str = comboBox1.Text;
            CreateWord1 crea = new CreateWord1();
            crea.CreatWorkExplain(strtext, str, jdkz, jdkzrq, cs13, jdgckz1, jdgckz2, jdgckz3, jdgccg1, jdgccg2, jdgccg3, cs6, cs7,dmdata,dt2,cs11,cs12,dmzd,bhtj);
        }
        #endregion

        #region
        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {
            String chartPath = @"E:\淮沐新河项目\工作\输出成果图\" + comboBox1.Text + "\\垂直位移横断面分布图";

            //第一种方法
            var files = Directory.GetFiles(chartPath, "*.*");
            foreach (var file in files)
                cs6.Add(file);
        }
        #endregion

        private void checkBox7_CheckedChanged(object sender, EventArgs e)
        {
            String chartPath = @"E:\淮沐新河项目\工作\输出成果图\" + comboBox1.Text + "\\垂直位移过程线";
            //第一种方法
            var files = Directory.GetFiles(chartPath, "*.*");
            foreach (var file in files)
                cs7.Add(file);
        }

        private void checkBox11_CheckedChanged(object sender, EventArgs e)
        {
            String chartPath = @"E:\淮沐新河项目\工作\输出成果图\" + comboBox1.Text + "\\河道断面比较图";
            //第一种方法
            var files = Directory.GetFiles(chartPath, "*.*");
            foreach (var file in files)
                cs11.Add(file);
        }

        private void checkBox12_CheckedChanged(object sender, EventArgs e)
        {
            String chartPath = @"E:\淮沐新河项目\工作\输出成果图\" + comboBox1.Text + "\\地形图";
            //第一种方法
            var files = Directory.GetFiles(chartPath, "*.*");
            foreach (var file in files)
                cs12.Add(file);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            string sql1 = "select distinct JdDate from jd_info where BrakeId='" + comboBox1.Text + "'";
            System.Data.DataTable dt1 = GetDataTable(sql1);
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                comboBox12.Items.Add(dt1.Rows[i][0].ToString());
            }
        }
    }
}
